package com.sc.nft.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dto.IdRequest;
import com.sc.nft.entity.SysMember;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.dto.*;
import com.sc.nft.entity.vo.*;
import com.sc.nft.enums.AuditMallBillStatusEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.SysMemberPool;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.MallBillFinancialAuditService;
import com.sc.nft.service.MallBillService;
import com.sc.nft.service.MallBillStatusLogService;
import com.sc.nft.service.MallOrderReconsiderService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

/**
 * @author liGen
 * @version 1.0
 * @date 2024/1/20 11:23 上午
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/mall/bill")
@Api(tags = "对账单模块")
public class MallBillController {

    private final MallBillService mallBillService;
    private final MallOrderReconsiderService mallOrderReconsiderService;
    private final MallBillStatusLogService mallBillStatusLogService;
    private final MallBillFinancialAuditService mallBillFinancialAuditService;


    @ApiOperation(value = "获取对账单列表", notes = "获取对账单列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<MallBillAdminPageDTO> getMallBillAdminPageDTO(@RequestBody MallBillAdminPageVO vo) {
        Page<MallBillAdminPageDTO> mallBillAdminPageDTO = mallBillService.getMallBillAdminPageDTO(vo);
        return PageResult.success(mallBillAdminPageDTO);
    }

    @ApiOperation(value = "获取对账单详情", notes = "获取对账单详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/detail", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<MallBillAdminPageDTO> getMallBillAdminPageDTO(@RequestBody IdRequest idRequest) {
        MallBillAdminPageDTO mallBillAdminPageDTO = mallBillService.getDetailById(idRequest.getId());
        return Result.success(mallBillAdminPageDTO);
    }

    @ApiOperation(value = "获取对账单订单", notes = "获取对账单列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/order/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<MallOrderBillAdminPageDTO> getMallBillAdminPageDTO(@RequestBody MallOrderBillAdminPageVO vo) {
        Page<MallOrderBillAdminPageDTO> mallOrderBillAdminPageDTOPage = mallBillService.getMallOrderBillAdminPageDTOByBillId(vo);
        return PageResult.success(mallOrderBillAdminPageDTOPage);
    }

    @ApiOperation(value = "获取对账单订单", notes = "获取对账单列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/excel/order/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<MallOrderBillAdminExcelDTO> getMallOrderBillAdminExcelDTO(@RequestBody MallOrderBillAdminPageVO vo) {
        Page<MallOrderBillAdminExcelDTO> mallOrderBillAdminExcelDTOPage = mallBillService.getMallOrderBillAdminExcelDTOByBillId(vo);
        return PageResult.success(mallOrderBillAdminExcelDTOPage);
    }


    @ApiOperation(value = "对账单审核(运营审核非财务审核)", notes = "对账单审核(运营审核非财务审核)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/audit", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result audit(@RequestBody AuditBillVO auditBillVO) {
        if (AuditMallBillStatusEnum.financialAuditList().contains(auditBillVO.getStatus())) {
            throw new GlobalRunTimeException("财务审核不支持");
        }
        mallBillService.audit(auditBillVO);
        return Result.success();
    }

    @ApiOperation(value = "订单复议列表", notes = "订单复议列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/order/reconsider/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<MallOrderReconsiderAdminPageDTO> getMallOrderReconsiderAdminPageDTO(@RequestBody MallOrderReconsiderAdminPageVO vo) {
        Page<MallOrderReconsiderAdminPageDTO> page = mallOrderReconsiderService.getPage(vo);
        return PageResult.success(page);
    }

    @ApiOperation(value = "订单复议详情", notes = "订单复议详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/order/reconsider/detail", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<MallOrderReconsiderAdminDetailDTO> getReconsiderDetail(@RequestBody IdRequest idRequest) {
        MallOrderReconsiderAdminDetailDTO detail = mallOrderReconsiderService.getDetail(idRequest.getId());
        return Result.success(detail);
    }

    @ApiOperation(value = "订单复议审核", notes = "订单复议审核", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/order/reconsider/audit", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result reconsiderAudit(@RequestBody AuditMallOrderReconsiderVO vo) {
        SysMember member = SysMemberPool.getMember();
        mallOrderReconsiderService.audit(member.getId(), vo);
        return Result.success();
    }

    @ApiOperation(value = "账单最新状态详情", notes = "订单复议详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/LatestStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<MallBillStatusLogDTO> LatestStatus(@RequestBody IdRequest idRequest) {
        MallBillStatusLogDTO theLatestStatus = mallBillStatusLogService.getTheLatestStatus(idRequest.getId());
        return Result.success(theLatestStatus);
    }

    @ApiOperation(value = "账单财务审核列表", notes = "账单财务审核列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/financialAudit/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<MallBillFinancialAuditPageDTO> getMallBillAdminPageDTO(@RequestBody MallBillFinancialAuditVO vo) {
        Page<MallBillFinancialAuditPageDTO> mallBillFinancialAuditPageDTO = mallBillFinancialAuditService.getMallBillFinancialAuditPageDTO(vo);
        return PageResult.success(mallBillFinancialAuditPageDTO);
    }

    @ApiOperation(value = "财务审核", notes = "财务审核", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/financial/audit", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result financialAudit(@RequestBody AuditBillFinancialAuditVO vo) {
        mallBillFinancialAuditService.audit(vo);
        return Result.success();
    }

}
